<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>库存</title>
<link rel="stylesheet" type="text/css"
	href=${pageContext.servletContext.contextPath}/themes/default/easyui.css>
<link rel="stylesheet" type="text/css"
	href=${pageContext.servletContext.contextPath}/themes/icon.css>
<link rel="stylesheet" type="text/css"
	href=${pageContext.servletContext.contextPath}/themes/bootstrap/easyui.css>
<link rel="stylesheet" 
	href=${pageContext.servletContext.contextPath}/js/bootstrap.min.css>
<script type="text/javascript"
	src=${pageContext.servletContext.contextPath}/js/jquery.min.js></script>
<script type="text/javascript"
	src=${pageContext.servletContext.contextPath}/js/bootstrap.min.js></script>
<script type="text/javascript"
	src=${pageContext.servletContext.contextPath}/js/jquery.easyui.min.js></script>
<script type="text/javascript"
	src=${pageContext.servletContext.contextPath}/js/easyui-lang-zh_CN.js></script>
</head>
<script type="text/javascript">
	$(function() {
		$('#msg').dialog('close');
		$('#search').dialog('close');
		var id = 0;
		$('#dg').datagrid({
			url : '${pageContext.servletContext.contextPath}/loadStore',
			striped : true,
			pageSize : 10,
			pageList : [ 5, 10, 15, 20 ],
			pagination : true,
			loadMsg : '正在加载',
			columns : [ [ 
			   {field : 'cb',checkbox : true,width : 100,align : 'center',halign : 'center'
			}, {field : 'fid',title : 'ID',width : 200,align : 'center',halign : 'center'
			}, {field : 'fname',title : '菜名',width : 300,align : 'center',halign : 'center'
			}, {field : 'fnums',title : '库存量',width : 300,align : 'center',halign : 'center'
			} ] ],
			toolbar : [{
				text : '重载库存列表',
				iconCls : 'icon-mini-refresh',
				width : 200,
				align : 'center',
				halign : 'center',
				handler : function() {
					$.messager.confirm('警告', '重载之后库存量都将清零，您确定要重载列表吗？', function(r){
						if (r){
							$.ajax({
								url : "${pageContext.servletContext.contextPath}/overloadStore",
								dataType : "json",
								type : "post",
								success : function(r) {
									if (r.flag != 0) {
										$('#dg').datagrid('reload');
										$.messager.alert('提示','操作成功！','info');
									} else {
										$.messager.alert('提示','操作失败！','info');
									}
								}
							})
						    
						}
					});
					}
				},'-',{
					text : '刷新库存',
					iconCls : 'icon-reload',
					width : 200,
					align : 'center',
					halign : 'center',
					handler : function() {
						$('#dg').datagrid('reload');
					}
				},'-',{
					text : '修改',
					iconCls : 'icon-edit',
					width : 200,
					align : 'center',
					halign : 'center',
					handler : function() {
						var cks = $('#dg').datagrid(
								'getSelections');
						if (cks.length == 1) {
							$.each(cks, function(i, n) {
								id = n.fid;
								$("#name").text(n.fname);
								$("#num").val(n.fnums);
							})
							$('#msg').dialog('open');
						} else {
							$.messager.alert('提示','请选择一行进行修改！', 'info');
						}
					}
				}, '-', {
					text : '搜索',
					iconCls : 'icon-search',
					width : 200,
					align : 'center',
					halign : 'center',
					handler : function() {
						$('#search').dialog('open');
					}
				} ]
			});
		//确定点击事件
		$("#qd").click(function() {		
			var fnums = $("#num").val();
			$('#msg').dialog('close');
			$("#name").text("");
			$("#num").val("");
			$.ajax({
				url : "${pageContext.servletContext.contextPath}/alterStore",
				dataType : "json",
				async : true,
				data : {
					"fid" : id,
					"fnums" : fnums
				},
				type : "post",
				success : function(r) {
					if (r.flag != 0) {
						$('#dg').datagrid('reload');
						$.messager.alert('提示', '操作成功！', 'info');
					} else {
						$.messager.alert('提示', '操作失败！', 'info');
					}
				}
			});	
		});
		//取消点击事件
		$("#qx").click(function() {
			$('#msg').dialog('close');
			$("#name").text("");
			$("#num").val("");
		});
		//确认搜索点击事件
		$("#ss").click(function() {		
			var fname = $("#fname").val();
			$('#search').dialog('close');
			$("#fname").val("");
			$.ajax({
				url : "${pageContext.servletContext.contextPath}/searchStore",
				dataType : "json",
				async : true,
				data : {
					"fname" : fname
				},
				type : "post",
				success : function(r) {
					$.each(r,function(i,n){
						$("#dg").datagrid("loadData",{ rows : n });			
					})
					
				}
			});
		});
		
	});
			
</script>
<body>
	<table id="dg"></table>
	<div id="msg" class="easyui-dialog" title="修改库存" style="width:400px;height:200px;"   
        data-options="iconCls:'icon-save',resizable:true,modal:true">       
		<div class="input-group">
			<span id="name" class="input-group-addon"></span> <input id="num" type="text"
				class="form-control" placeholder="库存量"/> <span
				class="input-group-addon">份</span>		 
		</div>	
		<div>
			<a id="qd" class="easyui-linkbutton" iconCls="icon-ok"
					style="width: 50%; height: 32px">确定</a> 
			<a id="qx" class="easyui-linkbutton" iconCls="icon-no"
					style="width: 49%; height: 32px">取消</a>
		</div>
	</div>
	<div id="search" class="easyui-dialog" title="搜索" style="width:300px;height:200px;"   
        data-options="iconCls:'icon-save',resizable:true,modal:true">       
		<div class="input-group">
			<span class="input-group-addon">菜名:</span><input id="fname" type="text"
				class="form-control"/>	 
		</div>	
		<div>
			<a id="ss" class="easyui-linkbutton" iconCls="icon-search"
					style="width: 100%; height: 32px">搜一搜</a> 
		</div>
	</div>  
</body>
</html>